package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private  DeviceDataMapper deviceDataMapper;


    @Override
    public PageResponse<DeviceDataVo> selectPageDeviceData(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //前端传过来的开始结束日期是毫秒值，转换为对应的LocalDateTime
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        if (startTime != null) {
            deviceDataPageReqDto.setStartDate(LocalDateTimeUtil.of(startTime));
        }
        if (endTime != null) {
            deviceDataPageReqDto.setEndDate(LocalDateTimeUtil.of(endTime));
        }
        //调用Mapper层查询数据
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        PageResponse<DeviceDataVo> response = PageResponse.of(page, DeviceDataVo.class);
        return response;
    }
}
